<script setup lang="ts">
  import { ref, defineProps, reactive, defineExpose } from "vue"
  import { createMessage } from '@/Public-UIlibrary/pb-message/method'
  import { getMealTimesSave, getMealTimesEdit } from '@/api/System'
  import dayjs from "dayjs"
  let emits = defineEmits('onConfirmModal')
  let loading = ref(false)
  const initialFrom = {
    mealTimesId: "",
    mealTimesName: "",
    mealTimesCode: '',
    beginTime: "",
    endTime: ""
  }
  let mealForm = ref({
    ...initialFrom
  })

  const mealFormRef = ref()
  let SetMealModalShow = ref(false)
  let disableTime = ref('00:00:00')
  const onShow = (row) => {
    console.log(row);

    if (row) {
      // let { parentTenantLevelName, createTime, ...needFrom } = row
      mealForm.value = { ...row }
      console.log('2024-01-01' + row.endTime);

      mealForm.value.beginTime = new Date('2024-01-01 ' + row.beginTime)
      mealForm.value.endTime = new Date('2024-01-01 ' + row.endTime)
      console.log(mealForm.value);

    }

    SetMealModalShow.value = true
  }
  const onConfirmFn = () => {
    mealFormRef.value.validate((valid) => {
      if (valid) {
        if (mealForm.value.mealTimesId) {
          getMealTimesEditApi()
        } else {
          getMealTimesSaveApi()
        }

      } else {

      }
    })
  }
  // 关闭表单重置
  const cancelFn = () => {
    mealFormRef.value.resetFields();
    disableTime.value = '00:00:00'
    Object.assign(mealForm.value, initialFrom);
  }
  // 开始时间改变
  const beginTimeChange = () => {
    console.log(mealForm.value.beginTime, 111);
    disableTime.value = dayjs(mealForm.value.beginTime).format('HH:mm:ss')
    if (mealForm.value.endTime && mealForm.value.beginTime.getTime() > mealForm.value.endTime.getTime()) {
      mealForm.value.endTime = ''
    }
    console.log(disableTime.value);
  }
  // 修改时间传参
  const getTime = (time) => {
    const [hours, minutes, seconds] = dayjs(time).format('HH:mm:ss').split(':').map(num => parseInt(num, 10));
    return {
      hours,
      minutes,
      seconds,
      nano: 0
    }
  }
  const getMealTimesSaveApi = async () => {
    loading.value = true
    let { beginTime, endTime, ...params } = mealForm.value
    params.beginTime = dayjs(beginTime).format('HH:mm:ss')
    params.endTime = dayjs(endTime).format('HH:mm:ss')
    let res = await getMealTimesSave({ ...params })
    loading.value = false
    if (res.code == 2000) {
      createMessage({
        message: '添加成功',
        type: 'success',
      })
      SetMealModalShow.value = false
      emits('onConfirmModal')
    }
  }
  const getMealTimesEditApi = async () => {
    loading.value = true
    let { beginTime, endTime, ...params } = mealForm.value
    params.beginTime = dayjs(beginTime).format('HH:mm:ss')
    params.endTime = dayjs(endTime).format('HH:mm:ss')
    let res = await getMealTimesEdit({ ...params })
    loading.value = false
    if (res.code == 2000) {
      createMessage({
        message: '编辑成功',
        type: 'success',
      })
      SetMealModalShow.value = false
      emits('onConfirmModal')
    }
  }
  defineExpose({ onShow })
</script>

<template>
  <pb-popup v-model="SetMealModalShow" :title="mealForm.mealTimesId ? '编辑' : '新增'" Width="365" @confirmFn="onConfirmFn"
    @cancelFn="cancelFn">
    <el-form :model="mealForm" ref="mealFormRef" label-width="auto" v-loading="loading">
      <el-form-item label="餐段名称" prop="mealTimesName" :rules="[
                 {
          required: true,
          pattern: /^[\u4e00-\u9fa5A-Za-z0-9]{2,8}$/,
          message: '请输入2-8位中文、数字、字母',

          trigger: 'blur',
        },  ]">
        <pb-input v-model=" mealForm.mealTimesName" placeholder="请输入餐段名称" />
      </el-form-item>
      <el-form-item label="开始时间" prop="beginTime" :rules="[
            {
              required: true,
              message: '请输入开始时间',
              trigger: 'blur',
            }, ]">
        <pb-time-picker v-model=" mealForm.beginTime" @change="beginTimeChange" placeholder="请输入开始时间" />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime" :rules="[
            {
              required: true,
              message: '请输入结束时间',
              trigger: 'blur',
            }, ]">
        <pb-time-picker :disabledTime="disableTime" v-model=" mealForm.endTime" placeholder="请输入结束时间" />
      </el-form-item>
    </el-form>
  </pb-popup>
</template>

<style lang="less" scoped>

</style>